package com.jerkin.stream;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @version V1.0
 * @ClassName: MapStream
 * @Description:
 * @author: huhongzhi
 * @date: 2020/8/5 15:50
 */
public class MapStream {

    public static void main(String[] args) {
        List<Map<String, String>> values = new ArrayList<>();
        for(int i=0;i<500;i++){
            Map<String, String> map = new HashMap<>();
            map.put(i+"",i+"");
            values.add(map);
        }

//        map.put("a", "a");
//        Map<String, String> map1 = new HashMap<>();
//        map1.put("b", "b");
//        Map<String, String> map2 = new HashMap<>();
//        map2.put("c", "c");
//        Map<String, String> map3 = new HashMap<>();
//        map3.put("d", "d");
//        Map<String, String> map4 = new HashMap<>();
//        map4.put("e", "e");
//        Map<String, String> map5 = new HashMap<>();
//        map5.put("f", "f");
//        Map<String, String> map6 = new HashMap<>();
//        map6.put("g", "g");
//        Map<String, String> map7 = new HashMap<>();
//        map7.put("h", "h");
//        Map<String, String> map8 = new HashMap<>();
//        map8.put("i", "i");
//        List<Map<String, String>> list = Lists.newArrayList(map, map1, map2, map3, map4, map5, map6, map7, map8);
        Map<String, String> map9 = new HashMap<>();
        AtomicInteger atomicInteger = new AtomicInteger();
        AtomicInteger atomicInteger1 = new AtomicInteger();

        Map<String, String> result = values.parallelStream().reduce(map9, (mapx, mapy) -> {
            atomicInteger1.incrementAndGet();
            System.out.println("thred"+Thread.currentThread().getName()+"初始值:"+mapx+"第二个参数:"+mapy);
            mapy.putAll(mapx);
            return mapy;
        }, (mapx, mapy) -> {
            atomicInteger.incrementAndGet();
            mapx.putAll(mapy);
            return mapx;
        });
        System.out.println("单次合并次数"+atomicInteger1.intValue());

        System.out.println("执行合并次数"+atomicInteger.intValue());
        System.out.println(result);
    }
}
